import { uniFetch } from '../api/uni-fetch'
import { useUserStore } from "/store/user.js"
import { onShareAppMessage } from "@dcloudio/uni-app"


// 登录
export const useLogin = () => {
	// console.log('useLogin');
	let loginInfo = ''
	uni.login({
		success: async (res) => {
			// console.log(res.code, 'uni.login==>res.code');
			if (res.code) {
				// console.log('uni.login==>code', res.code);
				//发起网络请求
				const user = await uniFetch.post('/user/login', {
					code: res.code,
					appId: uni.appId
				})
				// console.log(user, '/user/login');
				// user===>openid,session_key  未注册

				if (!user.data.user) {
					// console.log('走注册');
					uni.setStorageSync('userLoginCode', user.data)
					return useRegister()
				}

				const member = await uni.request({
					url: uniFetch.baseURL + '/user/user-member',
					method: 'POST',
					header: {
						token: user.data.token
					}
				})
				uni.setStorageSync('userLoginCode', user.data)
				const store = useUserStore()
				store.token = uni.getStorageSync('userLoginCode').token
				store.userInfo = uni.getStorageSync('userLoginCode').user
				// console.log(member, 'is_member');
				store.memberInfo = member.data.data
				if (store.curretPage) {
					// console.log(store.curretPage, '登录后跳转');
					uni.reLaunch({
						// url:'/subpkg/CreatOrder/CreatOrder?num=1&goodsId=290'
						url: '/' + String(store.curretPage)
					})
					return store.curretPage = ''
				}

			} else {
				// console.log('uni.login==>code登录失败!' + res.errMsg)
				uni.showToast({
					icon: 'none',
					title: res.errMsg
				})
			}
		},
		fail(err) {
			// console.log('uni.login==>code登录失败fail!' + err.errMsg)
			uni.showToast({
				icon: 'none',
				title: err.errMsg
			})
		}
	})
	loginInfo = uni.getStorageSync('userLoginCode')

	// console.log(loginInfo, 'loginInfo');
	return loginInfo
}


// 注册
export const useRegister = () => {
	// console.log('useRegister', uni.getStorageSync("userLoginCode").openid);
	uni.getUserProfile({
		desc: '为了完善服务，请求授权基本信息',
		success: (res) => {
			// console.log(uni.getStorageSync('invite_token'), '祖册传参');
			uniFetch.post("/user/register", {
				// openid: 'oxR-j67D-PjlG7ceiESgdfgvLUzOd0',
				openid: uni.getStorageSync("userLoginCode").openid,
				userInfo: res.userInfo,
				appId: uni.appId,
				invite_token: uni.getStorageSync('invite_token') || ''
			}).then(async res => {
				// console.log("register", res);
				if (res.code === 20002) {
					// console.log(20002);
					return uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}

				const member = await uni.request({
					url: uniFetch.baseURL + '/user/user-member',
					method: 'POST',
					header: {
						token: res.data.token
					}
				})

				uni.setStorageSync("userLoginCode", res.data);

				const store = useUserStore()
				store.token = uni.getStorageSync('userLoginCode').token
				store.userInfo = uni.getStorageSync('userLoginCode').user
				// console.log(member, 'is_member');
				store.memberInfo = member.data.data

				if (store.curretPage) {
					uni.showToast({ title: '登录成功' })
					// console.log(store.curretPage, '注册后跳转');
					uni.reLaunch({
						// url:'/subpkg/CreatOrder/CreatOrder?num=1&goodsId=290'
						url: '/' + String(store.curretPage)
					})
					return store.curretPage = ''
				} else {
					uni.switchTab({ url: '/pages/index/index' })
				}

				return res
				// return wx.restartMiniProgram({
				// 	path: '/pages/index/index'
				// })
			}, reject => {
				// console.log(reject, "reject注册报错")
			});
		},
		fail(err) {
			uni.showToast({
				icon: 'none',
				title: '登录享更多优惠'
			})
			// console.log(err, "fail注册报错");
		}
	});

}


// 页面分享

export const usePageShare = (path, title = "华靓甄选-首页") => {
	onShareAppMessage(() => {
		return {
			title,
			path
		}
	})
}